<?php


namespace app\api\controller\v1;


use app\api\exception\BaseException;
use app\api\validate\ClassScene;
use app\api\validate\PositionScene;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;

/**
 * Class AccountPosition
 * @package app\api\controller\v1
 * 用户职位
 */
class AccountPosition extends BaseException
{
    /**
     * 不需要登录方法
     */
    protected $noAuth = ['login'];

    /**
     * 【超级管理员】
     *  查询职位列表 翻页
     * 员工、财务、老板
     */

    public function getAccountPositionList(){
        $data = Request::param();
        if ($this->user_id > 2) {
            return self::Error(null, "当前账号无查看用户信息权限", 10002);
        }
        $fieldStr = "id,class_id,name,data_state,create_time,update_time";
        $search = [
        ];
        /**
         * 单一模糊查询开始
         */
        //名称模糊查询
        if (isset($data["name"]) && !empty($data["name"])) {
            $search[] = ["name", "like", "%" . $data["name"] . "%"];
        }

        /**
         * 单一模糊查询结束
         */
        /**
         *多条件查询开始
         */
        //数据状态筛选查询
        //  if (isset($data["data_state"]) && $data["data_state"] != '') {
        array_push($search, ["data_state", "=", 1]);
        //}

        if (isset($data["class_id"]) && $data["class_id"] != '') {
            array_push($search, ["class_id", "=", $data["class_id"]]);
        }
        $positiolist = Db::name("account_position")->where($search)->field($fieldStr)->order("id asc")->page($this->page_num, $this->page_limit)->select()->toArray();
        $count = Db::name("account_position")->where($search)->count();
        foreach ($positiolist as $key => $value){
            $account_class_name = Db::name("account_class")->where(["id" => $positiolist[$key]["class_id"],"data_state" => 1])->value("name");
            $positiolist[$key]["class_name"] = empty($account_class_name) ? "暂无分类":$account_class_name;
        }
        $list = [
            "total" => $count,
            "page" => $this->page_num,
            "limit" => $this->page_limit,
            "list" => $positiolist
        ];
        //Db::name("account")->getLastSql();
        return self::Success($list, '获取成功', 200);
    }
    /**
     * 【公共接口】
     *  数据下拉框
     */
    public function publicPositionList(){
        $request = Request::param();
        $list = Db::name("account_position")->where(["class_id" => $request["group_id"],"data_state" => 1])->column("id,name,class_id");
        return self::Success($list, "获取成功", 200);
    }
    /**
     * 【超级管理员】
     *  添加职位
     * 员工、财务、老板
     */
    public function addAccountPosition(){
        $request = Request::param();
        try {
            validate(PositionScene::class)
                ->scene('add')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $check_one = Db::name("account_position")->where(["class_id" => $request["class_id"],"name" => $request["name"]])->find();
        if ($check_one){
            return self::Error(null, '同一分类下，名称不能相同', 10002);
        }
        $check = Db::name("account_class")->where(["id" => $request["class_id"],"data_state" => 1])->find();
        if (!$check){
            return self::Error(null, '分类不存在', 10002);
        }
        $insertData = [
            "name" => $request["name"],
            "class_id" => $request["class_id"],
            'create_time' => date("Y-m-d H:i:s", time()),
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => 1
        ];
        $result = Db::name("account_position")->insert($insertData);
        if ($result) {
            return self::Success(null, "添加成功", 200);
        } else {
            return self::Error(null, '添加失败', 10002);
        }
    }

    /**
     * 【超级管理员】
     * 假删除 通过数据下来筛选
     * 更改职位的数据状态
     * 如果删除的话 需更新所有所属职位的用户的分类字段
     */
    public function updateAccountPositionState(){
        $request = Request::param();
        try {
            validate(PositionScene::class)
                ->scene('update_state')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $updateData = [
            'update_time' => date("Y-m-d H:i:s", time()),
            'data_state' => $request["data_state"]
        ];
        $check = Db::name("account_position")->where(["id" => $request["id"]])->find();
        if ($check) {
            $result = Db::name("account_position")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "操作成功", 200);
            } else {
                return self::Error(null, '操作失败', 10002);
            }
        } else {
            return self::Error(null, '职位不存在', 10002);
        }
    }
    /**
     * 【超级管理员】
     * 更改职位数据
     * 更改职位的数据状态
     * 如果删除的话 需更新所有所属职位的用户的分类字段
     */
    public function updateAccountPositionInfo(){
        $request = Request::param();
        try {
            validate(PositionScene::class)
                ->scene('update')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $check_one = Db::name("account_position")->where(["class_id" => $request["class_id"],"name" => $request["name"]])->find();
        if ($check_one){
            return self::Error(null, '同一分类下，名称不能相同', 10002);
        }
        $check = Db::name("account_class")->where(["id" => $request["class_id"],"data_state" => 1])->find();
        if (!$check){
            return self::Error(null, '分类不存在', 10002);
        }
        $check = Db::name("account_position")->where(["id" => $request["id"]])->find();
        if ($check) {
            $updateData = [
                "class_id" => $request["class_id"],
                "name" => $request["name"],
                'update_time' => date("Y-m-d H:i:s", time()),
            ];
            $result = Db::name("account_position")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "更新成功", 200);
            } else {
                return self::Error(null, '更新失败或已更新', 10002);
            }
        } else {
            return self::Error(null, '职位不存在', 10002);
        }
    }
}